/**
 * @file    Interrupt.c
 * @author  zhouh (17596492750@163.com)
 * @brief   中断服务函数具体实现
 * @version 0.1
 * @date    2025-05-21
 */

#include <MyProject.h>

extern uint8 data g_1mTick; // 1ms滴答信号，每隔1ms在SYSTICK定时器被置1，需在大循环使用处清零
extern unsigned char tim3_10ms_flag;
extern unsigned short tim3_can_send_cnt;

uint16 xdata spidebug[4] = {0}; // SPI debug 输出通道缓存，SPI调试器会将该变量值进行输出

/**
 * @brief   低于预警电压中断与过温中断
 *          开启低压检测中断后,MCU会对输入电压进行监测,当输入电压低于设定值，则会触发中断
 * @date    2022-12-29
 */
void LVW_TSD_INT(void) interrupt 0 // LVW & TSD interrupt
{
    if (ReadBit(LVSR, LVWIF))
    {
        if (ReadBit(LVSR, LVWF))
        {
            mcFaultSource = FaultUnderVoltageDC;
            ClrBit(LVSR, LVWF);
        }

        ClrBit(LVSR, LVWIF);
    }
}

/**
 * @brief        外部中断0
 * @brief        一般用于响应IPM的FO过流信号
 * @date         2022-07-14
 */
void EXTERN0_INT(void) interrupt 1 //  外部中断0
{
    if (IF0)
    {
        IF0 = 0; // clear P00 interrupt flag
    }
}

/**
 * @brief        外部中断1
 * @date         2023-07-26
 */
void EXTERN1_INT(void) interrupt 2 //  外部中断1
{
    if (P1_IF)
    {
        if (foc_control.sleep_flag == 1)
        {
            foc_control.sleep_flag = 0;
        }
        P1_IF = 0; //
    }
}

/**
 * @brief   FOC中断(Drv中断),每个载波周期执行一次，用于处理响应较高的程序，中断优先级第二
 * @date    2022-07-14
 */
void DRV_ISR(void) interrupt 3
{
    if (ReadBit(DRV_SR, FGIF))
    {
        DRV_SR = (SYSTIE | FGIF | DCIM1 | SYSTIF) & (~FGIF);
    }

    if (ReadBit(DRV_SR, DCIF)) // 比较中断
    {
#if (DBG_MODE == DBG_SPI_SW) // 软件调试模式
        spidebug[0] = FOC__THETA;
        spidebug[1] = foc_control.align_angle;
        spidebug[2] = foc_control.align_angle_temp;
#endif

        DRV_SR = (DRV_SR | SYSTIF) & (~DCIF);
    }
}

#if (TAILWIND_MODE == RSDMethod)
/**
 * @brief        Timer2中断服务函数
 * @note         本例程中用于RSD顺逆风检测
 * @date         2022-07-14
 */
void TIM2_INT(void) interrupt 4
{

    if (ReadBit(TIM2_CR1, T2IP))
    {
        RsdProcess();
        ClrBit(TIM2_CR1, T2IP);
    }

    if (ReadBit(TIM2_CR1, T2IF)) // 溢出中断,用于判断静止,时间为349ms。
    {
        mcRsd.State = STATIC;
        mcRsd.Period = 65535;
        mcRsd.Speed = 0;
        mcRsd.SpeedUpdate = 1;
        ClrBit(TIM2_CR1, T2IF);
    }

    if (ReadBit(TIM2_CR1, T2IR))
    {
        ClrBit(TIM2_CR1, T2IR);
    }
}
#endif

#if (TAILWIND_MODE == BEMFMethod)
void TIM1_INT(void) interrupt 5
{

    if (ReadBit(TIM1_SR, T1PDIF))
    {
        BemfProcess();
        ClrBit(TIM1_SR, T1PDIF);
    }
    else if (ReadBit(TIM1_SR, T1BOIF))
    {
        ClrBit(TIM1_SR, T1BOIF);
    }
    else
    {
        TIM1_SR = 0; // 防止其他中断位误开启，导致中断无法退出
    }
}
#endif

/**
 * @brief     CAN 中断
 * @date      2022-12-22
 */
void CAN_INT(void) interrupt 8
{
    if (ReadBit(CAN_IFR, RXIF)) // 接收中断
    {
        can_read();
        SetBit(CAN_CR1, BUFRLS);
    }

    if (ReadBit(CAN_IFR, TXIF)) // 发送成功中断
    {
        ClrBit(CAN_IFR, TXIF);
    }

    if (ReadBit(CAN_IFR, OVIF)) // 溢出中断
    {
        ClrBit(CAN_IFR, OVIF);
    }

    if (ReadBit(CAN_IFR, ARBIF)) // 仲裁中断
    {
        ClrBit(CAN_IFR, ARBIF);
    }

    if (ReadBit(CAN_IFR, ERRIF)) // 错误类型中断
    {
        ClrBit(CAN_IFR, ERRIF);
    }
}

/**
 * @brief   TIMER3 中断服务函数
 * @date    2025-05-21
 */
void TIM3_INT(void) interrupt 9
{
    if (ReadBit(TIM3_CR1, T3IF))
    {
        tim3_can_send_cnt++;
        tim3_10ms_flag = 1;
        
        if (tim3_can_send_cnt > 10000)
        {
            tim3_can_send_cnt = 0; // 超时清零，防止溢出
        }
		
		
		
        ClrBit(TIM3_CR1, T3IF);    
    }
    
}

/**
 * @brief   SysTick 定时器终端，周期为 1ms
 * @date    2022-07-14
 */
void SYStick_INT(void) interrupt 10
{
    if (ReadBit(DRV_SR, SYSTIF)) // SYS TICK中断
    {
        g_1mTick = 1;
        DRV_SR = (DRV_SR | DCIF) & (~SYSTIF);
    }
}

/**
 * @brief   比较器硬件过流保护，该中断仅提供 故障码 赋值,用于状态机的切换。
 *          需要开启比较器CMP3  发生过流 自动清除MOE功能
 * @date    2022-07-14
 */

void CMP3_INT(void) interrupt 12
{
    if (ReadBit(CMP_SR, CMP3IF))
    {
        if (motor_ctrl_state != init_position_check)
        {
            mcFaultSource = FaultHardOVCurrent; // 硬件过流保护
        }

        ClrBit(CMP_SR, CMP3IF);
    }
}

/**
 * @brief        Lin通信
 * @date         2022-08-13
 */
void Lin_INT(void) interrupt 14
{
    if (ReadBit(LIN_CR, LINIE))
    {
        if (ReadBit(LIN_SR, LINREQ))
        {
            // LIN_Schdule();                                                             //读取ID号并处理信号
            SetReg(LIN_SR, LINDONE | LINIDLE | LINREQ, LINIDLE | LINDONE);
        }

        if (ReadBit(LIN_SR, LINDONE)) // 数据收发完成
        {
            SetReg(LIN_SR, LINDONE | LINIDLE | LINREQ, LINIDLE | LINREQ);
        }

        if (ReadBit(LIN_CSR, LINWAKEUP)) // 唤醒信号
        {
            SetReg(LIN_CSR, LINWAKEUP | CLRERR, CLRERR);
        }

        if (ReadBit(LIN_SR, LINIDLE)) // 总线空闲
        {
            SetReg(LIN_SR, LINDONE | L  INIDLE | LINREQ, LINDONE | LINREQ);
        }

        if (ReadBit(LIN_CSR, CLRERR)) // 错误中断
        {
            /*----- 获取错误源 -----*/
            // LIN_Error_Handing();
            ClrBit(LIN_CSR, CLRERR);
        }
    }
}
